package com.douqu.game.core.entity.activity;


import com.bean.core.buffer.ByteBuffer;
import com.douqu.game.core.config.activity.CampRobotConfig;
import com.douqu.game.core.config.sprite.MasterConfig;
import com.douqu.game.core.entity.EntityObject;
import com.douqu.game.core.entity.Player;
import com.douqu.game.core.factory.DataFactory;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @Author: Bean
 * @Description:
 * @Date: 2018-03-09 14:27
 */
public class MatchBaseInfo extends EntityObject
{

    private int serverId;

    private String objectIndex;

    private String name;

    private int lv;

    private String avatar;

    private int camp;

    private int master;

    private int fc;

    private int winRate;

    /**
     * 最近匹配的记录
     */
    private List<String> matchList;

    public MatchBaseInfo()
    {
        this.matchList = new CopyOnWriteArrayList<>();
    }

    public MatchBaseInfo(String objectIndex)
    {
        this.objectIndex = objectIndex;
    }

    public MatchBaseInfo(Player player, int serverId)
    {
        this();

        this.serverId = serverId;
        this.objectIndex = player.getObjectIndex();
        this.name = player.getName();
        this.lv = player.getLv();
        this.avatar = player.avatar;
        this.camp = player.camp;
        this.master = player.master;
        this.fc = player.fc;
        this.winRate = player.getCampBattleData().getCurWinRate();

//        this.matchList.addAll(player.getCampBattleData().getMatchList());
    }

    public MatchBaseInfo(MatchBaseInfo playerBase)
    {
        this();

        this.serverId = playerBase.getServerId();
        this.objectIndex = playerBase.getObjectIndex();
        this.name = playerBase.getName();
        this.lv = playerBase.getLv();
        this.avatar = playerBase.getAvatar();
        this.camp = playerBase.getCamp();
        this.master = playerBase.getMaster();
        this.fc = playerBase.getFc();
        this.winRate = playerBase.getWinRate();
        this.matchList.addAll(playerBase.getMatchList());
    }

    public MatchBaseInfo(CampRobotConfig robot, int winRate)
    {
        this();

        this.objectIndex = robot.getId() + "";
        this.name = robot.getName();
        this.lv = robot.masterLv;
        MasterConfig masterConfig = DataFactory.getInstance().getGameObject(DataFactory.MASTER_KEY, robot.master);
        this.avatar = masterConfig.avatar;
        this.camp = 0;
        this.master = robot.master;
        this.fc = robot.fc;
        this.winRate = winRate;
    }

    public MatchBaseInfo(CampRobotConfig robot)
    {
        this();

        this.objectIndex = robot.getId() + "";
        this.name = robot.getName();
        this.lv = robot.masterLv;
        MasterConfig masterConfig = DataFactory.getInstance().getGameObject(DataFactory.MASTER_KEY, robot.master);
        this.avatar = masterConfig.avatar;
        this.camp = 0;
        this.master = robot.master;
        this.fc = robot.fc;
    }


    public void loadFrom(ByteBuffer buffer)
    {
        this.serverId = buffer.readInt();
        this.objectIndex = buffer.readUTF();
        this.name = buffer.readUTF();
        this.avatar = buffer.readUTF();
        this.lv = buffer.readShort();
        this.camp = buffer.readByte();
        this.master = buffer.readInt();
        this.fc = buffer.readInt();
        this.winRate = buffer.readByte();

        int size = buffer.readByte();
        for(int i = 0; i < size; i++)
        {
            matchList.add(buffer.readUTF());
        }
    }

    public void writeTo(ByteBuffer buffer)
    {
        buffer.writeInt(serverId);
        buffer.writeUTF(objectIndex);
        buffer.writeUTF(name);
        buffer.writeUTF(avatar);
        buffer.writeShort(lv);
        buffer.writeByte(camp);
        buffer.writeInt(master);
        buffer.writeInt(fc);
        buffer.writeByte(winRate);

        buffer.writeByte(matchList.size());
        for(String index : matchList)
            buffer.writeUTF(index);
    }

    public void addMatch(String targetIndex)
    {
        matchList.add(targetIndex);
        if(matchList.size() > 2)
            matchList.remove(0);
    }

    public void update(MatchBaseInfo playerBase)
    {
        this.name = playerBase.name;
        this.lv = playerBase.lv;
        this.avatar = playerBase.avatar;
        this.fc = playerBase.fc;
        this.winRate = playerBase.winRate;
//        this.matchList = playerBase.matchList;
    }

    public void update(Player playerBase)
    {
        this.objectIndex = playerBase.getObjectIndex();
        this.name = playerBase.getName();
        this.lv = playerBase.getLv();
        this.avatar = playerBase.avatar;
        this.camp = playerBase.camp;
        this.fc = playerBase.fc;
        this.winRate = playerBase.getCampBattleData().getCurWinRate();
    }



    public String getObjectIndex() {
        return objectIndex;
    }

    public void setObjectIndex(String objectIndex) {
        this.objectIndex = objectIndex;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getLv() {
        return lv;
    }

    public void setLv(int lv) {
        this.lv = lv;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }


    public int getCamp() {
        return camp;
    }

    public void setCamp(int camp) {
        this.camp = camp;
    }

    public int getWinRate() {
        return winRate;
    }

    public void setWinRate(int winRate) {
        this.winRate = winRate;
    }

    public List<String> getMatchList() {
        return matchList;
    }

    public void setMatchList(List<String> matchList) {
        this.matchList = matchList;
    }

    public int getFc() {
        return fc;
    }

    public void setFc(int fc) {
        this.fc = fc;
    }

    public int getServerId() {
        return serverId;
    }

    public void setServerId(int serverId) {
        this.serverId = serverId;
    }

    public int getMaster() {
        return master;
    }

    public void setMaster(int master) {
        this.master = master;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof MatchBaseInfo)) return false;

        MatchBaseInfo baseInfo = (MatchBaseInfo) o;

        if (!objectIndex.equals(baseInfo.objectIndex)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return objectIndex == null ? 0 : objectIndex.hashCode();
    }


    @Override
    public String toString() {
        return "{" +
                "serverId=" + serverId +
                ", objectIndex='" + objectIndex + '\'' +
                ", name='" + name + '\'' +
                ", lv=" + lv +
                ", camp=" + camp +
                ", fc=" + fc +
                ", winRate=" + winRate +
                "} ";
    }
}
